package freeboard.model.service;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpSession;

import library.dto.MemberDTO;

 

import org.springframework.web.multipart.MultipartFile;

import freeboard.dto.FreeBoardDTO;
import freeboard.dto.ListDTO;
import freeboard.dto.PagingDTO;
import freeboard.model.dao.FreeBoardDAO;
import freeboard.util.Utilities;

public class FreeBoardService {
	private FreeBoardDAO dao;
	private String pictureDir;//upload된 파일을 저장할 디렉토리 명
	public FreeBoardService(FreeBoardDAO dao, String pictureDir){
		this.dao = dao;
		this.pictureDir = pictureDir;
	}
	public void writeContent(FreeBoardDTO fdto,HttpSession session) throws SQLException, IOException{
		int no = dao.selectBoardNo();
		System.out.println("=] no: " + no);
		String content = Utilities.changeContentForDB(fdto.getContent());
		MemberDTO mdto = (MemberDTO)session.getAttribute("login_info");
		
		fdto.setNo(no);
		fdto.setContent(content);
		fdto.setId(mdto.getId());
		fdto.setWritedate(Utilities.getNow()); 
		fdto.setViewcount(0);
		fdto.setRefamily(no);
		fdto.setRestep(0);
		fdto.setRelevel(0);
		fdto.setWritedate(Utilities.changeDayTimeFormat(fdto.getWritedate()));
		
		MultipartFile file = fdto.getUpfile();
		if(!file.isEmpty()){
			String fileName = file.getOriginalFilename();//임시저장 메모리에 업로드된 파일 이름을 가져옴
			String extension = fileName.substring(fileName.lastIndexOf("."));//점 포함 확장자 부분을 의미
			long time = System.currentTimeMillis();
			String newFileName = time+extension;
			System.out.println(newFileName);
			file.transferTo(new File(pictureDir, newFileName));
			fdto.setPictureFilename(newFileName);
		}
		//5. DB에 insert
		dao.insertContent(fdto);
	}
	public ListDTO selectBoardListByPage(int page)	throws SQLException {
		ListDTO ldto = new ListDTO();
		int totalContent = dao.selectTotalContent();//총 게시물의 갯수
		System.out.println("page : "+page);
		System.out.println("totalContent:"+totalContent);
		ldto.setPagingTO(new PagingDTO(totalContent, page));//총게시물의 갯수와 현재 페이지를 보내서 페이징 처리에 필요한 각종 처리를 해준다.
		int a = Utilities.CONTENT_PER_PAGE;//한페이지 당 게시물수
		System.out.println("한페이지당 게시물수 : "+a);
		ArrayList<FreeBoardDTO> list = dao.selectBoardListByPage(page, a);
		System.out.println("list: "+list);
		for(FreeBoardDTO fdto : list){
			fdto.setWritedate(Utilities.changeDayTimeFormat(fdto.getWritedate()));
		}
		ldto.setList(list);
		return ldto;
	}
	public FreeBoardDTO getContentByNO(int no) throws SQLException {
		FreeBoardDTO fdto = null;
		dao.updateViewcount(no);
		fdto = dao.selectContentByNO(no);
		fdto.setWritedate(Utilities.changeDayFormat(fdto.getWritedate()));
		return fdto;
	}
	public FreeBoardDTO getNext(int no) throws SQLException {
		return dao.getNext(no);
	}
	
	public FreeBoardDTO getPrev(int no) throws SQLException {
		return dao.getPrev(no);
	}

		
	}

